"""绝对领域，多相册图片爬取"""
import os

import parsel
import requests

url = "https://www.jdlingyu.com/tuji?post_order=like"
base_dir = "E:\\T\\Picture\\jdlingyu\\"
res = requests.get(url)
html = res.text
# print(res.text)

selector = parsel.Selector(html)
# 获取相册列表的定位方式
pics_list = selector.xpath('//*[@id="post-list"]/ul/li')
for i in pics_list[1:11]:
    # 继续定位到需要的元素
    pics_title = i.xpath(".//h2/a/text()").get().split("#")[-1].strip()
    pics_href = i.xpath(".//h2/a/@href").get()
    # print(pics_title, pics_href)
    # 创建对应文件夹
    if not os.path.exists(base_dir + pics_title):
        os.mkdir(base_dir + pics_title)
    resp = requests.get(pics_href).text
    select = parsel.Selector(resp)
    pics = select.xpath('//div[@class="entry-content"]//img/@src').getall()
    a = 1
    for j in pics:
        data = requests.get(j).content
        with open(base_dir + pics_title + f"\\{a}.jpg", "wb") as f:
            f.write(data)
        a += 1
